package com.dhh.nutrition.activity;

import greendroid.app.GDActivity;
import greendroid.widget.ActionBarItem.Type;
import com.dhh.nutrition.R;
import com.dhh.nutrition.dao.DataBaseHelper;
import com.dhh.nutrition.manager.FoodManager;
import com.dhh.nutrition.model.NutritionFacts;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class NutritionFactsActivity extends GDActivity {	
	private NutritionFacts facts;
	private FoodManager fManager;
	private DataBaseHelper db;
	private int type = 0;
	private int serving = 100;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setActionBarContentView(R.layout.label);
        
        db = new DataBaseHelper(getBaseContext());
        db.openDataBase();        
        fManager = new FoodManager(db);
        
        if(getIntent().getExtras().getInt("serving")>0){
        	type = getIntent().getExtras().getInt("type");
        	serving = getIntent().getExtras().getInt("serving");
        }
        
		initActionBar();
    }
	
	@Override
	public void onStart(){
		super.onStart();
		this.initData();
		this.fillData();
	}
	
	@Override
	protected void onPause(){
		super.onPause();
		if (db != null) {
			db.close();
		}
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, 0, 0, "100 g");
		if(facts.getFood().getGmWt_1()>0)
			menu.add(0, 1, 0, facts.getFood().getGmWt_Desc1()+" ("+facts.getFood().getGmWt_1()+"g)");
		if(facts.getFood().getGmWt_2()>0)
			menu.add(0, 2, 0, facts.getFood().getGmWt_Desc2()+" ("+facts.getFood().getGmWt_2()+"g)");
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case 0:
			facts.setWeightType(100, 0);
			this.fillData();
			return true;
		case 1:
			facts.setWeightType(1, 1);
			this.fillData();
			return true;
		case 2:
			facts.setWeightType(1, 2);
			this.fillData();
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}
	
	public void initActionBar(){
		getActionBar().setTitle("Food Detail Info");
        getActionBar().setBackgroundColor(Color.rgb(154, 205, 50));
        getActionBar().getChildAt(0).
        	setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(NutritionFactsActivity.this,HomeActivity.class);
					startActivity(intent);
				}});
        
        getActionBar().addItem(Type.List).getItemView().setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(NutritionFactsActivity.this,NutritionDetailActivity.class);
				intent.putExtra("foodNo", getIntent().getExtras().getString("foodNo"));
				intent.putExtra("type", facts.getWeightType());
				intent.putExtra("serving", facts.getWeight());
				startActivity(intent);
			}});
        
        getActionBar().addItem(Type.Edit).getItemView().setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				registerForContextMenu( v ); 
				v.setLongClickable(false);
				NutritionFactsActivity.this.openContextMenu(v);
			}});
	}
	
	public void initData(){
		facts = fManager.getNutritionFact(getIntent().getExtras().getString("foodNo"));
		facts.setWeightType(serving, type);
	}	
	
	private void fillData(){
		((TextView) findViewById(R.id.nf_servingSize_w)).setText(facts.getServingSize());
		((TextView) findViewById(R.id.nf_servingSize_q)).setText(facts.getServingWeight());
		((TextView) findViewById(R.id.nf_calories)).setText(facts.getCalories());
		((TextView) findViewById(R.id.nf_calories_fat)).setText(facts.getCaloriesFromFat());
		((TextView) findViewById(R.id.nf_total_fat)).setText(facts.getTotalFat());
		((TextView) findViewById(R.id.nf_total_fat_dv)).setText(facts.getTotalFatDV());
		((TextView) findViewById(R.id.nf_fat_sat)).setText(facts.getSaturatedFat());
		((TextView) findViewById(R.id.nf_fat_sat_dv)).setText(facts.getSaturatedFatDV());
		((TextView) findViewById(R.id.nf_fat_trans)).setText(facts.getTransFat());
		((TextView) findViewById(R.id.nf_cholesterol)).setText(facts.getCholesterol());
		((TextView) findViewById(R.id.nf_cholesterol_dv)).setText(facts.getCholesterolDV());
		((TextView) findViewById(R.id.nf_sodium)).setText(facts.getSodium());
		((TextView) findViewById(R.id.nf_sodium_dv)).setText(facts.getSodiumDV());
		((TextView) findViewById(R.id.nf_carb)).setText(facts.getTotalCarbohydrate());
		((TextView) findViewById(R.id.nf_carb_dv)).setText(facts.getTotalCarbohydrateDV());
		((TextView) findViewById(R.id.nf_fiber)).setText(facts.getDietaryFiber());
		((TextView) findViewById(R.id.nf_fiber_dv)).setText(facts.getDietaryFiberDV());
		((TextView) findViewById(R.id.nf_sugars)).setText(facts.getSugars());
		((TextView) findViewById(R.id.nf_protein)).setText(facts.getProteins());
		((TextView) findViewById(R.id.nf_va_dv)).setText(facts.getVitamADV());
		((TextView) findViewById(R.id.nf_vc_dv)).setText(facts.getVitamCDV());
		((TextView) findViewById(R.id.nf_calcium_dv)).setText(facts.getCalclumDV());
		((TextView) findViewById(R.id.nf_iron_dv)).setText(facts.getIronDV());		
	}
}
